package com.RLFZ.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.RLFZ.entity.HealthClock;
import com.RLFZ.service.HealthClockService;
import com.RLFZ.vo.DataView;
import com.RLFZ.vo.HealthClockVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 服务控制器
 *
 * @author zh
 * @since 2022-08-25 17:16:58
 * @description 
 */
@Slf4j
@RequiredArgsConstructor
@Controller
@RequestMapping("/healthClock")
public class HealthClockController {

    @Autowired
    private HealthClockService healthClockService;
    @RequestMapping("/toHealthClock")
    public String toHealthClock(){
        return "admin/healthclock";
    }

    /**
     * 查询所有的打卡记录 带有模糊查询的条件 带有分页
     * @param healthClockVo
     * @return
     */
    @RequestMapping("/listHealthClock")
    @ResponseBody
    public DataView listHealthClock(HealthClockVo healthClockVo){

        // 查询所有带有模糊查询的条件 带有分页
        QueryWrapper<HealthClock> queryWrapper=new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNoneBlank(healthClockVo.getName()),"name",healthClockVo.getName());
        queryWrapper.eq(StringUtils.isNoneBlank(healthClockVo.getPhone()),"phone",healthClockVo.getPhone());
//        queryWrapper.like(healthClockVo.getUsername()!=null,"username",healthClockVo.getUsername());
//        queryWrapper.eq(healthClockVo.getPhone()!=null,"phone",healthClockVo.getPhone());
        IPage<HealthClock> page=new Page<>(healthClockVo.getPage(),healthClockVo.getLimit());
        // start
//        User user = (User)session.getAttribute("User");
//        System.out.println(user.getId()+"-------------");
//        QueryWrapper<HealthClock> wrapper = queryWrapper.select("id", user.getId());
        System.out.println(queryWrapper+"------------777777");
        // end
        healthClockService.page(page,queryWrapper);


//        queryWrapper.like(StringUtils.isNoneBlank(userVo.getUsername()),"username",userVo.getUsername());
//        queryWrapper.eq(StringUtils.isNoneBlank(userVo.getPhone()),"phone",userVo.getPhone());
        // 查询所有带有模糊查询的条件 带有分页

        return  new DataView(page.getTotal(),page.getRecords());
    }

    /**
     * 添加或更改数据
     * @param healthClock
     * @return
     */
    @RequestMapping("/addHealthClock")
    @ResponseBody
    public DataView addHealthClock(HealthClock healthClock){
        System.out.println(healthClock);
        boolean b = healthClockService.saveOrUpdate(healthClock);
        DataView dataView = new DataView();
        if (b){
            dataView.setCode(200);
            dataView.setMsg("添加成功!");
        }else {
            dataView.setCode(100);
            dataView.setMsg("添加失败");
        }
        return dataView;
    }

    /**
     * 删除成功
     * @param id
     * @return
     */
    @RequestMapping("/deleteHealthClockById")
    @ResponseBody
    public DataView deleteHealthClockById(Integer id){
        healthClockService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除数据成功!");
        return dataView;
    }

    /**
     * 批量删除
     * @param id
     * @return
     */
    @RequestMapping("/deleteListHealthClock")
    @ResponseBody
    public DataView deleteHealthClockById(Integer[] id){
        for (int i=0;i<id.length;i++){
            healthClockService.removeById(id[i]);
        }
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除数据成功!");
        return dataView;
    }

    /**
     *
     * @param healthClock
     * @return
     */
    @RequestMapping("/updateHealthClock")
    @ResponseBody
    public DataView updateHeSuan(HealthClock healthClock){
        DataView dataView=new DataView();
//        System.out.println(hesuan);
        Integer condition = condition(healthClock);
        // 年龄
        if (condition==101){
            dataView.setMsg("修改失败，年龄格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 身份证
//        if (condition==102){
//            dataView.setMsg("修改失败，身份证格式不符合规格");
//            dataView.setCode(100);
//            return dataView;
//        }
        // 手机号
        if (condition==103){
            dataView.setMsg("修改失败，手机号格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        // 姓名
        if (condition==104){
            dataView.setMsg("修改失败，姓名格式不符合规格");
            dataView.setCode(100);
            return dataView;
        }
        boolean save = healthClockService.updateById(healthClock);
        if (save){
            dataView.setMsg("修改成功");
            dataView.setCode(200);
        }
        else {
            dataView.setMsg("修改失败");
            dataView.setCode(100);
        }
        return dataView;
    }

    /**
     * 核酸检测输入条件判断方法
     * @param healthClock
     * @return
     */
    public Integer condition(HealthClock healthClock){
        Integer age = healthClock.getAge();
//        String card = HealthClock.getCard();
        String phone = healthClock.getPhone();
        String name = healthClock.getName();
        if (age==null||age<0||age>100){
            return 101;
        }
//        if (card==null||card.equals("")||card.length()!=18){
//            return 102;
//        }
        if (phone==null||phone.equals("")||phone.length()!=11){
            return 103;
        }
        if (name==null||name.equals("")||name.length()<1||name.length()>20){
            return 104;
        }
        return 200;
    }

}